home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / game / think / AmiChess.lha / AmiChess / src / mui_field.c < prev    next >
C/C++ Source or Header  |  2002-10-31  |  7KB  |  235 lines

  1. #include <clib/alib_protos.h>
  2. #include <clib/graphics_protos.h>
  3. #include <clib/intuition_protos.h>
  4. #include <clib/muimaster_protos.h>
  5. #include <clib/utility_protos.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "common.h"
  10.  
  11. extern ULONG pix_x,col_white,col_black;
  12. extern Object *skin;
  13. extern struct BitMap *skin_bm;
  14. extern PLANEPTR skin_mask;
  15.  
  16. struct Data
  17. {
  18. int num;
  19. };
  20.  
  21. struct MUI_CustomClass *MUI_Field_Class;
  22.  
  23. static char bw[64]={
  24. 0,1,0,1,0,1,0,1,
  25. 1,0,1,0,1,0,1,0,
  26. 0,1,0,1,0,1,0,1,
  27. 1,0,1,0,1,0,1,0,
  28. 0,1,0,1,0,1,0,1,
  29. 1,0,1,0,1,0,1,0,
  30. 0,1,0,1,0,1,0,1,
  31. 1,0,1,0,1,0,1,0
  32. };
  33.  
  34. static char nboard[64]={
  35. 56,57,58,59,60,61,62,63,
  36. 48,49,50,51,52,53,54,55,
  37. 40,41,42,43,44,45,46,47,
  38. 32,33,34,35,36,37,38,39,
  39. 24,25,26,27,28,29,30,31,
  40. 16,17,18,19,20,21,22,23,
  41. 8,9,10,11,12,13,14,15,
  42. 0,1,2,3,4,5,6,7
  43. };
  44.  
  45. static char rboard[64]={
  46. 7,6,5,4,3,2,1,0,
  47. 15,14,13,12,11,10,9,8,
  48. 23,22,21,20,19,18,17,16,
  49. 31,30,29,28,27,26,25,24,
  50. 39,38,37,36,35,34,33,32,
  51. 47,46,45,44,43,42,41,40,
  52. 55,54,53,52,51,50,49,48,
  53. 63,62,61,60,59,58,57,56
  54. };
  55.  
  56. static ULONG mFieldNew(struct IClass *cl,Object *obj,struct opSet *msg)
  57. {
  58. obj=DoSuperNew(cl,obj,
  59.     MUIA_FixWidth,pix_x,
  60.     MUIA_FixHeight,pix_x,
  61.     MUIA_Draggable,1,
  62.     MUIA_Dropable,1,
  63. TAG_MORE,msg->ops_AttrList);
  64. if(obj)
  65.     {
  66.     struct Data *data=(struct Data *)INST_DATA(cl,obj);
  67.     data->num=GetTagData(MUIA_UserData,0,msg->ops_AttrList);
  68.     }
  69. return(ULONG)obj;
  70. }
  71.  
  72. static ULONG mFieldAskMinMax(struct IClass *cl,Object *obj,struct MUIP_AskMinMax *msg)
  73. {
  74. struct MUI_MinMax *mm;
  75. DoSuperMethodA(cl,obj,(Msg)msg);
  76. mm=msg->MinMaxInfo;
  77. mm->MinWidth+=pix_x;
  78. mm->MinHeight+=pix_x;
  79. mm->MaxWidth+=pix_x;
  80. mm->MaxHeight+=pix_x;
  81. mm->DefWidth+=pix_x;
  82. mm->DefHeight+=pix_x;
  83. return 0;
  84. }
  85.  
  86. static ULONG mFieldDragQuery(struct IClass *cl,Object *obj,struct MUIP_DragQuery *msg)
  87. {
  88. ULONG from,to,retval=MUIV_DragQuery_Refuse;
  89. GetAttr(MUIA_UserData,msg->obj,&from);
  90. GetAttr(MUIA_UserData,obj,&to);
  91. from=flags&REVERSEBOARD?rboard[from]:nboard[from];
  92. to=flags&REVERSEBOARD?rboard[to]:nboard[to];
  93. if(from!=to)
  94.     {
  95.     int side=board.side;
  96.     if(board.b[side][pawn]&BitPosArray[from]) retval=MUIV_DragQuery_Accept;
  97.     else if(board.b[side][knight]&BitPosArray[from]) retval=MUIV_DragQuery_Accept;
  98.     else if(board.b[side][bishop]&BitPosArray[from]) retval=MUIV_DragQuery_Accept;
  99.     else if(board.b[side][rook]&BitPosArray[from]) retval=MUIV_DragQuery_Accept;
  100.     else if(board.b[side][queen]&BitPosArray[from]) retval=MUIV_DragQuery_Accept;
  101.     else if(board.b[side][king]&BitPosArray[from]) retval=MUIV_DragQuery_Accept;
  102.     if(retval==MUIV_DragQuery_Accept)
  103.         {
  104.         if(board.b[side][pawn]&BitPosArray[to]) retval=MUIV_DragQuery_Refuse;
  105.         else if(board.b[side][knight]&BitPosArray[to]) retval=MUIV_DragQuery_Refuse;
  106.         else if(board.b[side][bishop]&BitPosArray[to]) retval=MUIV_DragQuery_Refuse;
  107.         else if(board.b[side][rook]&BitPosArray[to]) retval=MUIV_DragQuery_Refuse;
  108.         else if(board.b[side][queen]&BitPosArray[to]) retval=MUIV_DragQuery_Refuse;
  109.         else if(board.b[side][king]&BitPosArray[to]) retval=MUIV_DragQuery_Refuse;
  110.         }
  111.     }
  112. return retval;
  113. }
  114.  
  115. static ULONG mFieldDragDrop(struct IClass *cl,Object *obj,struct MUIP_DragDrop *msg)
  116. {
  117. int side=board.side;
  118. ULONG from,to;
  119. leaf *ptr;
  120. char cmd[6];
  121. GetAttr(MUIA_UserData,msg->obj,&from);
  122. GetAttr(MUIA_UserData,obj,&to);
  123. from=flags&REVERSEBOARD?rboard[from]:nboard[from];
  124. to=flags&REVERSEBOARD?rboard[to]:nboard[to];
  125. cmd[0]='a'+(from%8);
  126. cmd[1]='1'+(from/8);
  127. cmd[2]='a'+(to%8);
  128. cmd[3]='1'+(to/8);
  129. cmd[4]=0;
  130. if(side==white&&cmd[1]=='7'&&cmd[3]=='8')
  131.     {
  132.     if(board.b[side][pawn]&BitPosArray[from])
  133.         {
  134.         strcat(cmd,"Q");
  135. //        puts("Promote white pawn to queen");
  136.         }
  137.     }
  138. else if(cmd[1]=='2'&&cmd[3]=='1')
  139.     {
  140.     if(board.b[side][pawn]&BitPosArray[from])
  141.         {
  142.         strcat(cmd,"Q");
  143. //        puts("Promote black pawn to queen");
  144.         }
  145.     }
  146. if(ptr=ValidateMove(cmd))
  147.     {
  148.     char text[50];
  149.     DoMethod(_app(obj),MUIM_Chess_Side);
  150.     SANMove(ptr->move,1);
  151.     MakeMove(board.side,&ptr->move);
  152.     strcpy(Game[GameCnt].SANmv,SANmv);
  153.     sprintf(text,"%d: %-5s",GameCnt/2+1,SANmv);
  154.     DoMethod(mui_app,MUIM_Chess_AddMove,1^board.side,text);
  155.     DoMethod(_app(obj),MUIM_Chess_ShowBoard);
  156.     DoMethod(_app(obj),MUIM_Chess_Side);
  157.     if(!(flags&SUPERVISOR))
  158.         {
  159.         DoMethod(_app(obj),MUIM_Chess_Side);
  160.         Iterate();
  161.         if(flags&ENDED) DoMethod(_app(obj),MUIM_Chess_ClearFlag,AUTOPLAY);
  162.         DisplayBeep(0);
  163.         }
  164.     }
  165. else DisplayBeep(0);
  166. return 0;
  167. }
  168.  
  169. static ULONG mFieldDraw(struct IClass *cl,Object *obj,Msg msg)
  170. {
  171. int sq,x;
  172. struct Data *data=(struct Data *)INST_DATA(cl,obj);
  173. struct RastPort *rp=_rp(obj);
  174. sq=flags&REVERSEBOARD?rboard[data->num]:nboard[data->num];
  175. SetAPen(rp,bw[sq]?col_white:col_black);
  176. RectFill(rp,_left(obj),_top(obj),_right(obj),_bottom(obj));
  177. if(board.b[white][pawn]&BitPosArray[sq])        x=6;
  178. else if(board.b[white][knight]&BitPosArray[sq]) x=8;
  179. else if(board.b[white][bishop]&BitPosArray[sq]) x=9;
  180. else if(board.b[white][rook]&BitPosArray[sq])   x=7;
  181. else if(board.b[white][queen]&BitPosArray[sq])  x=10;
  182. else if(board.b[white][king]&BitPosArray[sq])   x=11;
  183. else if(board.b[black][pawn]&BitPosArray[sq])   x=0;
  184. else if(board.b[black][knight]&BitPosArray[sq]) x=2;
  185. else if(board.b[black][bishop]&BitPosArray[sq]) x=3;
  186. else if(board.b[black][rook]&BitPosArray[sq])   x=1;
  187. else if(board.b[black][queen]&BitPosArray[sq])  x=4;
  188. else if(board.b[black][king]&BitPosArray[sq])   x=5;
  189. else x=-1;
  190. if(x>=0) BltMaskBitMapRastPort(skin_bm,x*pix_x,0,rp,_left(obj),_top(obj)+1,pix_x,pix_x-2,ANBC|ABNC|ABC,skin_mask);
  191. return 0;
  192. }
  193.  
  194. #ifdef __GNUC__
  195. static ULONG Dispatcher(struct IClass *cl __asm("a0"),Object *obj  __asm("a2"),Msg msg  __asm("a1"))
  196. #elif defined __VBCC__
  197. static ULONG Dispatcher(__reg("a0") struct IClass *cl,__reg("a2") Object *obj,__reg("a1") Msg msg)
  198. #else
  199. static ULONG Dispatcher(register __a0 struct IClass *cl,register __a2 Object *obj,register __a1 Msg msg)
  200. #endif
  201. {
  202. ULONG retval;
  203. switch(msg->MethodID)
  204.     {
  205.     case OM_NEW:
  206.         retval=mFieldNew(cl,obj,(struct opSet *)msg);
  207.         break;
  208.     case MUIM_AskMinMax:
  209.         retval=mFieldAskMinMax(cl,obj,(struct MUIP_AskMinMax *)msg);
  210.         break;
  211.     case MUIM_DragQuery:
  212.         retval=mFieldDragQuery(cl,obj,(struct MUIP_DragQuery *)msg);
  213.         break;
  214.     case MUIM_DragDrop:
  215.         retval=mFieldDragDrop(cl,obj,(struct MUIP_DragDrop *)msg);
  216.         break;
  217.     case MUIM_Draw:
  218.         retval=mFieldDraw(cl,obj,msg);
  219.         break;
  220.     default:
  221.         retval=DoSuperMethodA(cl,obj,msg);
  222.     }
  223. return retval;
  224. }
  225.  
  226. void INIT_6_MUI_Field_Class()
  227. {
  228. if(!(MUI_Field_Class=MUI_CreateCustomClass(0,MUIC_Area,0,sizeof(struct Data),(APTR)Dispatcher))) exit(20);
  229. }
  230.  
  231. void EXIT_6_MUI_Field_Class()
  232. {
  233. if(MUI_Field_Class) MUI_DeleteCustomClass(MUI_Field_Class);
  234. }
  235.